package de.koller.pulsar.scene.planet;

import de.koller.aframe.util.resource.IOLoader;
import de.koller.oceanFramework.gl.util.RasterManager;
import de.koller.oceanFramework.math.RVec3;
import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.pulsar.PNode;
import de.koller.pulsar.scene.IScene;
import de.koller.pulsar.util.InterceptPerspective;
import java.util.Random;

public class PlanetScene extends PNode implements IScene {

	public final Random RND = new Random();
	public final int WIDTH = 1920 / 2, HEIGHT = 1080 / 2;

	private RasterManager raster = new RasterManager( WIDTH, HEIGHT );		
	private InterceptPerspective perspective = InterceptPerspective.create( 60, WIDTH, HEIGHT );
	private StarRender starRender = new StarRender();	
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
	
		raster.clear( 0xff000000 );
		StarRender.Context[] c = new StarRender.Context[ 1000 ];
		
		for( int i = 0; i < c.length; i++ ) {
			RVec3 sp = perspective.project( new RVec3( 
					RND.nextInt( 5000 ) - 2500,
					RND.nextInt( 5000 ) - 2500,
					RND.nextInt( 10000 ) ), null );
			StarBrightness sb = new StarBrightness( 100, 1, perspective );
			c[i] = starRender.renderContext( sp.x, sp.y, sb.getValue( sp.z ), 0, 0xff_ffffff );
		}
		
		for( int x = 0; x < WIDTH; x++ ) {
			for( int y = 0; y < HEIGHT; y++ ) {
				int color = 0xff_000000;
				for( int i = 0; i < c.length; i++ )
					color = ColorIntUtil.add( c[i].colorAt( x, y ), color );
				raster.setPixel( color, x, y );
			}
			System.out.println( x + " / " + WIDTH );
		}
		
		proot.setSceneFrame( IOLoader.create( raster ) );
	}

}
